package cn.lll.vuehouse.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.lll.vuehouse.DTO.FileDTO;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Allfile;
import cn.lll.vuehouse.service.AllfileService;
import cn.lll.vuehouse.mapper.AllfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author asus-pc
* @description 针对表【allfile】的数据库操作Service实现
* @createDate 2023-09-03 11:01:52
*/
@Service
public class AllfileServiceImpl extends ServiceImpl<AllfileMapper, Allfile>
    implements AllfileService{


    @Autowired
    private AllfileMapper allfileMapper;

    @Override
    public ResponseResult getPage(FileDTO fileDTO) {
        LambdaQueryWrapper<Allfile> lambda = new QueryWrapper<Allfile>().lambda();
        lambda.eq(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(fileDTO.getFileName()), Allfile::getFileName, fileDTO.getFileName());

        Page<Allfile> allfilePage = new Page<>(fileDTO.getCurrentPage(), fileDTO.getLimit());

        allfileMapper.selectPage(allfilePage,lambda);

        List<Allfile> records = allfilePage.getRecords();
        long total = allfilePage.getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("pageData",records);

        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }

    @Override
    public ResponseResult upload(MultipartFile file) {
        //判断是否有文件上传
        if(file.isEmpty()){
            ResponseResult responseResult = new ResponseResult();
            responseResult.fail(ResponseStatus.FAIL);
            return responseResult;
        }

        //文件上传  io 文件名唯一
        String originalFilename = file.getOriginalFilename();
        System.out.println("originalFilename = " + originalFilename);

        //获取文件扩展名
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        //uuid  xxxx-xx-xxxx
        //UUID.randomUUID().toString().replace("-","");
        String uuid = IdUtil.simpleUUID();
        String uuidName = uuid + "." + filenameExtension;
        File file1 = new File("D:\\upload\\" + uuidName);


        try {
            file.transferTo(file1);

            Allfile allfile = new Allfile();
            allfile.setCreateTime(new Date());

            //去后缀
            allfile.setFileName(StringUtils.stripFilenameExtension(originalFilename));
            allfile.setUuidName(uuidName);

            //文件大小
            double size = file.getSize() / 1024;
            allfile.setSize(size);

            allfileMapper.insert(allfile);

            ResponseResult responseResult = new ResponseResult();
            responseResult.success(ResponseStatus.SUCCESS);
            return responseResult;

        } catch (IOException e) {
            throw new RuntimeException("文件上传失败!!!!!!!!");
        }

    }

    @Override
    public void down(String fileName, HttpServletResponse response) {

        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\upload\\" + fileName);
            //设置响应格式  ，以附件形式打开
            response.setContentType("application/force-download");//强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);

            ServletOutputStream outputStream = response.getOutputStream();
            IoUtil.copy(fileInputStream,outputStream);
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}




